package com.pd.modules.server.dialogs;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.pd.modules.common.logging.LogSingleton;
import com.pd.modules.server.Server;

public class DialogMain
{

	DialogMain window;
	private JFrame frame;
	JButton btnStart = new JButton("INICIAR");
	JButton btnStop = new JButton("PARAR");

	DialogListConnections mDialogConnections = new DialogListConnections();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					DialogMain window = new DialogMain();
					window.frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DialogMain()
	{
		window = this;
		initialize();

		LogSingleton.getInstance().showLogView(true);
		mDialogConnections.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize()
	{
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 224, 188);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		btnStart.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{

				DialogNetworkInterfaces dialog = new DialogNetworkInterfaces(window);
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			}
		});
		btnStart.setFont(new Font("Tahoma", Font.BOLD, 24));
		btnStop.setEnabled(false);

		btnStop.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Server.getInstance().stop();
				btnStop.setEnabled(false);
				btnStart.setEnabled(true);
			}
		});
		btnStop.setFont(new Font("Tahoma", Font.BOLD, 24));
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
				Alignment.TRAILING,
				groupLayout
						.createSequentialGroup()
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(
								groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(btnStart, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE)
										.addComponent(btnStop, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE))
						.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
				groupLayout.createSequentialGroup().addContainerGap()
						.addComponent(btnStart, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(btnStop, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		frame.getContentPane().setLayout(groupLayout);

		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("Ficheiro");
		menuBar.add(mnFile);

		JMenuItem mntmSair = new JMenuItem("Sair");
		mnFile.add(mntmSair);

		JMenu mnGerir = new JMenu("Gerir");
		menuBar.add(mnGerir);

		JMenuItem mntmUtilizadores = new JMenuItem("Utilizadores");
		mntmUtilizadores.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				DialogUsers dialog = new DialogUsers();
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			}
		});
		mnGerir.add(mntmUtilizadores);

		JMenuItem mntmSalas = new JMenuItem("Salas");
		mntmSalas.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				DialogRooms dialog = new DialogRooms(null);
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			}
		});
		mnGerir.add(mntmSalas);

		JMenuItem mntmReservas = new JMenuItem("Reservas");
		mntmReservas.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				DialogReservations dialog = new DialogReservations();
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			}
		});
		mnGerir.add(mntmReservas);

		JMenu mnOpes = new JMenu("Ver");
		menuBar.add(mnOpes);

		JCheckBoxMenuItem chkMostrarLog = new JCheckBoxMenuItem("Log");
		chkMostrarLog.setSelected(true);
		chkMostrarLog.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				AbstractButton aButton = (AbstractButton) event.getSource();
				boolean selected = aButton.getModel().isSelected();

				if (selected)
				{
					LogSingleton.getInstance().showLogView(true);
				} else
				{
					LogSingleton.getInstance().showLogView(false);
				}
			}
		});
		mnOpes.add(chkMostrarLog);

		JCheckBoxMenuItem chckbxmntmLigaes = new JCheckBoxMenuItem("Liga\u00E7\u00F5es");
		chckbxmntmLigaes.setSelected(true);
		chckbxmntmLigaes.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				AbstractButton aButton = (AbstractButton) e.getSource();
				boolean selected = aButton.getModel().isSelected();

				if (selected)
				{
					mDialogConnections.setVisible(true);
				} else
				{
					mDialogConnections.setVisible(false);
				}
			}
		});
		mnOpes.add(chckbxmntmLigaes);
	}
}
